package top.ucat.starter.dubbo.common;

import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;

/**
 * @Auther: Jun
 * @Date: 2019/9/20 10:35
 * @Description:
 */
public class DubboUtil {


    private static ApplicationConfig applicationConfig = new ApplicationConfig();

    public static ReferenceConfig getReferenceConfig(Class cla, RegistryConfig registryConfig) {
        ReferenceConfig referenceConfig = new ReferenceConfig<>();
        referenceConfig.setApplication(applicationConfig);
        referenceConfig.setRegistry(registryConfig);
        referenceConfig.setInterface("");
        referenceConfig.setInterface(cla);
        referenceConfig.setVersion(registryConfig.getVersion());
        referenceConfig.setGroup(registryConfig.getGroup());
        return referenceConfig;
    }

    public static <T> T getDubboBean(Class<T> cla, RegistryConfig registryConfig) {
        ReferenceConfig<T> referenceConfig = DubboUtil.getReferenceConfig(cla, registryConfig);
        return referenceConfig.get();
    }

    public static <T> T getDubboBean(String className, RegistryConfig registryConfig) throws ClassNotFoundException {
        Class aClass = Class.forName(className);
        return (T) DubboUtil.getDubboBean(aClass, registryConfig);
    }
}
